package April;

/**
 * @author:gao
 * @date:2020/7/6
 * @time:9:15
 * @version:0.0.1
 * @description:none
 */

//876. 链表的中间结点
public class MiddleNode {
    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode listNode = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);

        listNode.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next= null;
        //solution.middleNode()
    }

}
 //Definition for singly-linked list.
  class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
    }

class Solution {
    public ListNode middleNode(ListNode p) {
        ListNode head = new ListNode(-1);
        head.next=p;
        ListNode slow = head;
        ListNode fast = head;
        while (fast!=null&&fast.next!=null){
            slow=slow.next;
            fast = fast.next.next;
        }
        if(fast==null) return slow;

        return  slow.next;
    }
}